

/****************************/
/*                          */ 
/* Create lawyer identifier */
/*                          */ 
/****************************/



libname raw 'G:\Data\Rawdata\707677';

libname stats 'G:\Data\Rawdata\707677\Eksterne data'; 

libname common 'G:\Data\Workdata\707677\Common\01Data\01SAS';






/***********************************************/
/* Load data and select variables that we need */
/***********************************************/



/* Data from all messages in Statstidende associated with people in our sample */


proc sql;

create table list as select 

a.pnr, a.granted, a.aar, a.pyear, a.pmonth, b.court, b.lawyerid

from common.list as a left join

common.stats as b on

a.pnr=b.pnr;

quit;




/*****************************************/
/* Identify one lawyer per unique person */
/*****************************************/


data lawyer;

set list;

keep pnr lawyerid;

run;



data lawyer;

set lawyer;

if lawyerid~=.;

run;



proc sort data=lawyer;

by pnr lawyerid;

run;



data lawyer;

set lawyer;

count+1;

by pnr lawyerid;

if first.pnr or first.lawyerid then count=1;

run;




proc sort data=lawyer;

by pnr DESCENDING count;

run;



proc sort data=lawyer nodupkey;

by pnr;

run;



data lawyer;

set lawyer;

drop count;

rename lawyerid=majority;

run;


proc sql;

create table list as select *

from list as a left join

lawyer as b on

a.pnr=b.pnr;

quit;



data list;

retain pnr aar pyear pmonth granted lawyerid majority;

set list;

run;


data list;

set list;

drop lawyerid;

rename majority=lawyerid;

run;



proc datasets library=work nolist;

delete lawyer;

quit;





/***********************************************/
/* Compute fraction of granted cases by lawyer */
/***********************************************/




data lawyer;

set list;

keep pnr lawyerid granted;

if lawyerid~=.;

run;



proc sort data=lawyer nodupkey;

by pnr;

run;



proc sort data=lawyer;

by lawyerid;

run;



proc summary data=lawyer;

by lawyerid;

output out=stats

n(granted)=obs

sum(granted)=sumgrant;

run;



data stats;

set stats;

drop _type_ _freq_;

run;



data people;

set list;

keep pnr lawyerid granted;

if lawyerid~=.;

run;



proc sort data=people nodupkey;

by pnr;

run;




proc sql;

create table people as select *

from people as a left join

stats as b on

a.lawyerid=b.lawyerid;

quit;



data people;

set people;

meanlawyer=ROUND((sumgrant-granted)/(obs-1),0.001);

run;



data people;

set people;

keep pnr meanlawyer obs;

run;



data people;

set people;

rename obs=obs_lawyer;

run;



proc sql;

create table list as select *

from list as a left join

people as b on

a.pnr=b.pnr;

quit;



/* Produce datasets for individuals */


data instruments;

set list;

run;



proc sort data=instruments nodupkey;

by pnr;

run;




data common.ivs;

set instruments;

run;




proc datasets library=work nolist kill;

quit;

















